package com.small.act;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.small.R;
import com.small.adapter.ShopNewsAdapter;
import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.AsyncDataLoader.Callback;
import com.small.model.ShopNewsModel;

public class ShopNewsActivity extends Activity {

	private ListView list_view;
	private ShopNewsAdapter list_ada;

	private ImageButton btn_back;

	private Action action;
	private int result = 0;
	List<ShopNewsModel> shopList = null;
	private HashMap<String, Object> favShopMap = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
	 
		super.onCreate(savedInstanceState);
		 
		setContentView(R.layout.activity_shopnews_view);
		action = new Action(ShopNewsActivity.this);
		findView();
		initData();
	}

	public void findView() {
		btn_back = (ImageButton) this.findViewById(R.id.btn_back);
		btn_back.setOnClickListener(onClick);
		list_view = (ListView) this.findViewById(R.id.listview);
	}

	private void initData() {

		action.showProgressDialog("正在提交...");
		new AsyncDataLoader(asyncCallback).execute();
	}

	private Callback asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {
//			favShopMap = action.myFavShop(Common.model.getMemberID(), 10, 1);
			favShopMap = action.getShopNews("2", 5, 1);
		}

		@Override
		public void onPrepare() {
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onFinish() {
			action.stopProgressDialog();
			if (favShopMap != null) {
				Integer i = (Integer) favShopMap.get("result");
				result = (int) i;
				shopList = (List<ShopNewsModel>) favShopMap.get("shopList");

				resultMethod(result, shopList); 
				
			}  else{
				resultMethod(-2,null);
			}
		}
	};

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.btn_back:// 返回
				finish();
				break;
			}
		}
	};

	protected void resultMethod(int result2, List<ShopNewsModel> shopList2) {
		switch (result2) {
		case 0:
			action.showShortToast("请求失败，请稍候再试...");
			break;
		case 1:
			if (shopList != null) {
				list_ada = new ShopNewsAdapter(ShopNewsActivity.this,shopList);
				list_view.setAdapter(list_ada);
			} else {
				action.showShortToast("请求失败，请稍候再试...");
			}
			break;
		case -2:
			action.showShortToast("数据异常，请求失败...");
			break;

		default:
			break;
		}

	}
}
